//
//  selLanguage.m
//  SysMenu
//
//  Created by superking on 10-8-23.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "selLanguage.h"
#import "MusicRequire.h"
#import "FileTools.h"


@implementation selLanguage

+(id) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	selLanguage *layer = [selLanguage node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

//设置第一次进入游戏文件   为1表示第一次   
/*CWriter FirstTime;
FirstTime.WriteInt(1);
FirstTime.Close("GameTime");*/


-(id)init{
	self=[super init];
	if (self) {
		self.isTouchEnabled = YES;		
		//背景
		bg=[CCSprite spriteWithFile:@"back_Hiphop.png"];
		bg.anchorPoint=CGPointZero;		
		[self addChild:bg z:0 tag:1];
		
		//选项背景框
		bgEng=[CCSprite spriteWithFile:@"Select-Language_box.png" rect:CGRectMake(0, 0, 162, 28)];
		bgEng.position=ccp(240,210);
		bgSimChs=[CCSprite spriteWithFile:@"Select-Language_box.png" rect:CGRectMake(0, 0, 162, 28)];
		bgSimChs.position=ccp(240,160);
		bgComChs=[CCSprite spriteWithFile:@"Select-Language_box.png" rect:CGRectMake(0, 0, 162, 28)];
		bgComChs.position=ccp(240,110);	
		[self addChild:bgEng z:1 tag:2];
		[self addChild:bgSimChs z:1 tag:3];
		[self addChild:bgComChs z:1 tag:4];
		
		//选项
		CCSprite *Eng=[CCSprite spriteWithFile:@"Select-Language_Word.png" rect:CGRectMake(0, 0, 60, 14)];
		Eng.position=ccp(240,210);
		CCSprite *SimChs=[CCSprite spriteWithFile:@"Select-Language_Word.png" rect:CGRectMake(0, 14, 60, 14)];		
		SimChs.position=ccp(240,160);
		CCSprite *ComChs=[CCSprite spriteWithFile:@"Select-Language_Word.png" rect:CGRectMake(0, 28, 60, 14)];		
		ComChs.position=ccp(240,110);
		[self addChild:Eng z:2 tag:2];
		[self addChild:SimChs z:2 tag:3];
		[self addChild:ComChs z:2 tag:4];		
		
	}
	return self;
}
	
CWriter SaveLanguage;
//English操作
-(void)selEng{
	[bgEng setTextureRect:CGRectMake(0, 28, 162, 28)];	
	SaveLanguage.WriteInt(1);
	SaveLanguage.Close("GameLanguage");
	[[CCDirector sharedDirector] replaceScene:[CCShrinkGrowTransition transitionWithDuration:0.5f scene:[MusicRequire scene]]];
			
}

//简体中文操作
-(void)selSimChs{
	[bgSimChs setTextureRect:CGRectMake(0, 28, 162, 28)];	
	SaveLanguage.WriteInt(2);
	SaveLanguage.Close("GameLanguage");
	[[CCDirector sharedDirector] replaceScene:[CCShrinkGrowTransition transitionWithDuration:0.5f scene:[MusicRequire scene]]];
}

//繁体中文操作
-(void)selComChs{
	[bgComChs setTextureRect:CGRectMake(0, 28, 162, 28)];	
	SaveLanguage.WriteInt(3);
	SaveLanguage.Close("GameLanguage");
	[[CCDirector sharedDirector] replaceScene:[CCShrinkGrowTransition transitionWithDuration:0.5f scene:[MusicRequire scene]]];	
}


//取屏幕相应区域调用函数，改变选项背景框
-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
}

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{
	
	
	CGPoint location = [touch locationInView: [touch view]];
	CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
	
	//English
	CGRect EngRect = CGRectMake(bgEng.position.x-bgEng.contentSize.width/2, bgEng.position.y-bgEng.contentSize.height/2, bgEng.contentSize.width, bgEng.contentSize.height);
	if (CGRectContainsPoint(EngRect, convertedLocation)) {
		[self selEng];
	}
	
	//简体中文
	CGRect SimChsRect = CGRectMake(bgSimChs.position.x-bgSimChs.contentSize.width/2, bgSimChs.position.y-bgSimChs.contentSize.height/2, bgSimChs.contentSize.width, bgSimChs.contentSize.height);
	if (CGRectContainsPoint(SimChsRect, convertedLocation)) {
		[self selSimChs];
	}	
	
	//繁体中文
	CGRect ComChsRect = CGRectMake(bgComChs.position.x-bgComChs.contentSize.width/2, bgComChs.position.y-bgComChs.contentSize.height/2, bgComChs.contentSize.width, bgComChs.contentSize.height);
	if (CGRectContainsPoint(ComChsRect, convertedLocation)) {
		[self selComChs];
	}		
	
	
	return YES;
}

- (void)ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
	//CGPoint location = [touch locationInView: [touch view]];
	//CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
	
}

- (void)ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
{
	//CGPoint location = [touch locationInView: [touch view]];
	//CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
	
}



@end
